package com.hj.saapp.view;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import com.hj.saapp.BCDApplication;
import com.hj.saapp.R;
import com.hj.saapp.control.adapter.ListHorizontalDialogAdapter;
import com.hj.saapp.control.listener.ListHorizontalDialogListener;

import java.lang.reflect.Field;
import java.util.ArrayList;

/**
 * Created by Administrator on 2015/10/28.
 */
@SuppressLint("ValidFragment")
public class ListHorizontalDialog extends DialogFragment {
    ArrayList<String> mData;
    ArrayList<String> cData;
    String title;

    ListHorizontalDialogAdapter pAdapter;
    ListHorizontalDialogAdapter cAdapter;

    ListHorizontalDialogListener listener;

    int p1 = 0, p2 = 0;


    private LinearLayout mViewDefaultingDialogMLinearLayout;
    private TextView mViewDefaultingDialogTitleTv;
    private ListView mViewDefaultingDialogFatherLv;
    private ListView mViewDefaultingDialogChildLv;

    private void assignViews(View view) {
        mViewDefaultingDialogMLinearLayout = (LinearLayout) view.findViewById(R.id.view_defaulting_dialog_mLinearLayout);
        mViewDefaultingDialogTitleTv = (TextView) view.findViewById(R.id.view_defaulting_dialog_titleTv);
        mViewDefaultingDialogFatherLv = (ListView) view.findViewById(R.id.view_defaulting_dialog_fatherLv);
        mViewDefaultingDialogChildLv = (ListView) view.findViewById(R.id.view_defaulting_dialog_childLv);
    }


    public ListHorizontalDialog(String title, ArrayList<String> mData,ArrayList<String> cData, ListHorizontalDialogListener listener)
    {
        this.mData = mData;
        this.cData = cData;
        this.title = title;
        this.listener = listener;
    }

    @Override
    public void onStart()
    {
        super.onStart();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        getDialog().getWindow().setLayout(dm.widthPixels - 30, getDialog().getWindow().getAttributes().height);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        return inflater.inflate(R.layout.view_multi_list_dialog,null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        assignViews(view);
        initView();
    }
    private void initView()
    {
        mViewDefaultingDialogTitleTv.setText(title);
        pAdapter = new ListHorizontalDialogAdapter(BCDApplication.getApp(),mData);
        cAdapter = new ListHorizontalDialogAdapter(BCDApplication.getApp(),cData);
        mViewDefaultingDialogFatherLv.setAdapter(pAdapter);
        mViewDefaultingDialogChildLv.setAdapter(cAdapter);
        mViewDefaultingDialogChildLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                p2 = position;
                cAdapter.name = cData.get(position);
                listener.onListItemClick(p1, position);
                cAdapter.notifyDataSetChanged();
            }
        });
        mViewDefaultingDialogFatherLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                p1 = position;
                pAdapter.name = mData.get(position);
                listener.onListItemClick(position, -1);
                pAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onDetach() {

        super.onDetach();



        try {

            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");

            childFragmentManager.setAccessible(true);

            childFragmentManager.set(this, null);



        } catch (NoSuchFieldException e) {

            throw new RuntimeException(e);

        } catch (IllegalAccessException e) {

            throw new RuntimeException(e);

        }

    }
    public void update()
    {
        pAdapter.notifyDataSetChanged();
        cAdapter.notifyDataSetChanged();
    }
}
